<?php
class User
{
	public $exitCode = 2;
	public $exitText = "";
	public $userName = "";
	public $userId = "";
	public $userPass = "";
	private $userVerified = false;
	
	public function setUserPass($user = "",$pass = "")
	{
		$this->userName = $user;
		$this->userPass = $pass;
		$existCheck = crowFather("select count(*) from user where userName = " . crowFather($user,6) . ";",5);
		if($existCheck == 1)
		{
			$this->userExists = true;
		}
		else
		{
			$this->userExists = false;
		}
		
		$this->argsSet = false;
		if($this->userName != "")
		{
			if($this->userPass != "")
			{
				$this->argsSet = true;
			}
		}
		
		if($this->checkPassword())
		{
			$this->passValid = true;
		}
		else
		{
			$this->passValid = false;
		}
		
	}
	
	public function giveExitStatus($exitCode,$exitStatus)
	{
		$this->exitCode = $exitCode;
		$this->exitText = $exitStatus;
	}
	
	
	public function checkPassword()
	{
		if(isset($this->argsSet))
		{
			if($this->argsSet)
			{
				$this->passwordHashQuery = "select userPassword from user where userName = " . crowFather($this->userName,6) . ";";
				$this->passwordHashAnswer = crowFather($this->passwordHashQuery,5);
				if(hash('sha256',$this->userPass) == $this->passwordHashAnswer)
				{
					crowFather("update user set userLastLogin = now() where userName =" . crowFather($this->userName,6) . ";",3);
					$this->passValid = true;
					return true;
				}
				else
				{
					$this->passValid = false;
					return false;
				}
			}
		}
		else
		{
			$this->giveExitStatus(1,"Use setUserPass to set the user up first");
		}
	}
	
	public function addUser()
	{
		#check if user exists
		if($this->argsSet)
		{
			$userExists = crowFather("select count(*) from user where userName = " . $this->userName . ";",5);
			if($userExists == 0)
			{
				#supported in PHP5.5
				#crowFather("insert into user(userName,userPassword) values('" . $this->userName . "','" . password_hash($this->userPassword,PASSWORD_BCRYPT) . "');",3);
				
				crowFather("insert into user(userName,userPassword) values('" . $this->userName . "','" . hash('sha256',$this->userPass) . "');",3);
				$this->giveExitStatus(0,"The user was added successfully");
			}
			else
			{
				$this->giveExitStatus(1,"The user already exists");
			}
		}
		else
		{
			$this->giveExitStatus(1,"The user was not defined");
		}
		#if dosen't exist , create it
		#exit with failure if user exists
	}
	
	public function deleteUser()
	{
		if($this->argsSet)
		{
			crowFather("delete from message where messageSender = " . crowFather($this->userName,6) . " or messageRecipient = " . crowFather($this->userName,6) . ";",3);
			crowFather("delete from user where userName = " . crowFather($this->userName,6) . ";",3);
			$this->giveExitStatus(0,"The user was deleted");
		}
		else
		{
			$this->giveExitStatus(1,"The user was not instantiated");
		}
	}
	
	public function changePass()
	{
		if($this->argsSet)
		{

			if(crowFather("select count(*) from passChange where requestUser = " . crowFather($this->userName,6) . ";",5) == 0)
			{
				crowFather("insert into passChange (requestUser,requestToken) values (" . crowFather($this->userName,6) . ",MD5(RAND()));",3);
				$authCode = crowFather("select requestToken from passChange where requestUser = " . crowFather($this->userName,6) . ";",5);
				$authEmail = crowFather("select emailAddress from user where userName = " . crowFather($this->userName,6) . ";",5);
				mail($authEmail,"Password Reset","A password reset token has been created for you on chatboss. Please find it below:\r\n\r\n" . crowFather($authCode,6));
				$this->giveExitStatus(0,"Password reset token has been set");
			}
			else
			{
				$this->giveExitStatus(1,"A reset token already exists");
			}
		}
		else
		{
			$this->giveExitStatus(1,"The user object was not properly instantiated");
		}
	}
	
	public function authChangePassToken($authToken)
	{
		if($this->argsSet)
		{
			if(crowFather("select count(*) from passChange where requestUser = " . crowFather($this->userName,6) . ";",5) == 1)
			{
				if(crowFather("select count(*) from passChange where requestUser = " . crowFather($this->userName,6) . " and requestToken = " . crowFather($authToken,6) . ";",5) == 1)
				{
					crowFather("delete from passChange where requestUser = " . crowFather($this->userName,6) . ";",3);
					crowFather("update user set userPassword = " . crowFather(hash('sha256',$this->userPass),6) . " where userName = " . crowFather($this->userName,6) . ";",3);
					$this->giveExitStatus(0,"Password has been changed");
				}
				else
				{
					$this->giveExitStatus(1,"The token given was incorrect");
				}
			}
			else
			{
				$this->giveExitStatus(1,"No reset token exists for this user");
			}
		}
		else
		{
			$this->giveExitStatus(1,"The user object was not instantiated properly");
		}
	}
	
}
?>